package org.indi.graduatemanagement.controller;


import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.indi.graduatemanagement.common.dto.UserDto;
import org.indi.graduatemanagement.common.lang.Result;
import org.indi.graduatemanagement.entity.User;
import org.indi.graduatemanagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zcl
 * @since 2021-04-25
 */
@RestController
@RequiresAuthentication
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequiresRoles("admin")
    @GetMapping("/{id}")
    @RequiresAuthentication
    public Result getUserById(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        Assert.notNull(user, "用户不存在");
        return Result.successful(user);
    }

    @RequiresRoles("admin")
    @GetMapping("/all")
    @RequiresAuthentication
    public Result getAllUser() {
        List<User> userList = userService.list();
        return Result.successful(userList);
    }

    @RequiresRoles("admin")
    @PostMapping("/save")
    @RequiresAuthentication
    public Result createUser(@Validated @RequestBody UserDto userDto) {
        User userTemp = userService.getOne(new QueryWrapper<User>().eq("userName", userDto.getUserName()));
        if(userTemp!=null){
            return Result.fail("用户名已存在");
        }
        User user = new User(SecureUtil.md5(userDto.getPassword()), userDto.getRoles(), userDto.getPermission(), userDto.getUserName());
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//设置日期格式
        LocalDateTime dateTime = LocalDateTime.now();
        String stringDateTime = df.format(dateTime);
        user.setCreated(LocalDateTime.parse(stringDateTime,df));
        user.setStatus(0);
        user.setLastLogin(dateTime);
        userService.save(user);
        return Result.successful(user);
    }

    @RequiresRoles("admin")
    @PostMapping("/update")
    @RequiresPermissions("grant")
    @RequiresAuthentication
    public Result updateUser(@Validated @RequestBody UserDto userDto) {
        if (!userService.getById(userDto.getId()).getPassword().equals(userDto.getPassword())){
            userDto.setPassword(SecureUtil.md5(userDto.getPassword()));
        }
        User user = new User(userDto.getId(), userDto.getPassword(), userDto.getRoles(),
                userDto.getPermission(), userDto.getStatus(), userDto.getCreated(), userDto.getLastLogin(),
                userDto.getUserName());
        userService.updateById(user);
        return Result.successful(null);
    }

    @RequiresRoles("admin")
    @DeleteMapping("/{id}")
    @RequiresAuthentication
    public Result deleteUser(@PathVariable("id") Long id) {
        Assert.notNull(userService.getById(id), "用户不存在");
        userService.removeById(id);
        return Result.successful(null);
    }
}
